package com.leo.ui;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.texture.Texture;
import com.leo.common.Config;
import com.leo.common.GameType;
import com.leo.component.TankLevelComponent;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.layout.HBox;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

/**
 * 信息面板
 */
public class InfoPane extends VBox {

    public InfoPane() {
        setSpacing(50);
        setStyle("-fx-background-color: lightgray");
        setPrefSize(6 * Config.CELL_SIZE, FXGL.getAppHeight());
        setTranslateX(28 * Config.CELL_SIZE);
        setAlignment(Pos.TOP_CENTER);

        // 敌人剩余产生数量
        // 在vbox中的子组件，setPrefWidth不生效，所以需要使用setMaxWidth
        TilePane tp = new TilePane();
        VBox.setMargin(tp, new Insets(30, 0, 0, 0));
        tp.setAlignment(Pos.CENTER);
        tp.setHgap(10);
        tp.setVgap(6);
        tp.setMaxWidth(25 * 2 + 15);
        tp.setPrefHeight(25 * 10 + 6 * 10 + 15);
        tp.setStyle("-fx-border-color: red");
        for (int i = 0; i < Config.MAX_ENEMY_NUM - FXGL.geti("enemyNum"); i++) {
            tp.getChildren().add(FXGL.texture("ui/enemy_pre.png"));
        }

        getChildren().add(tp);

        // 关卡信息
        Texture levelPng = FXGL.texture("ui/levelFlag.png");
        Text text = new Text("" + FXGL.geti("level"));
        text.setFont(Font.font(26));
        HBox levelBox = new HBox(levelPng, text);
        levelBox.setMaxWidth(80);
        levelBox.setAlignment(Pos.BOTTOM_CENTER);
        getChildren().add(levelBox);

        // 子弹等级
        HBox bulletBox = new HBox();
        bulletBox.setMaxWidth(29 * 3 + 5);
        for (int i = 0; i <= Config.TANK_MAX_LEVEL; i++) {
            Texture bulletPng = FXGL.texture("ui/bulletLevel.png");
            bulletPng.setVisible(i == 0);
            bulletBox.getChildren().add(bulletPng);
        }
        getChildren().add(bulletBox);

        Entity entity = FXGL.getGameWorld().getEntitiesByType(GameType.PLAYER).get(0);
        entity.getComponent(TankLevelComponent.class).valueProperty().addListener((observable, oldValue, newValue) -> {
            for (int i = 0; i < newValue.intValue(); i++) {
                bulletBox.getChildren().get(i).setVisible(true);
            }
        });


        FXGL.getip("enemyNum").addListener((observable, oldValue, newValue) -> {
            ObservableList<Node> children = tp.getChildren();
            for (int i = children.size() - 1; i >= Config.MAX_ENEMY_NUM - newValue.intValue(); i--) {
                children.get(i).setVisible(false);
            }
        });
    }
}
